package us.icitap.commandcenter.ui.dispatcher;

import java.util.ArrayList;
import java.util.Date;

import us.icitap.commandcenter.CommandCenterUI;
import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.business.ejb.persistence.CallManagement;
import us.icitap.commandcenter.listeners.ManageCallListener;
import us.icitap.commandcenter.listeners.UpdateCallListener;
import us.icitap.commandcenter.ui.custom.ButtonsLayout;
import us.icitap.commandcenter.ui.custom.CustBeanFieldGroup;
import us.icitap.commandcenter.ui.custom.CustSaveButton;
import us.icitap.commandcenter.ui.custom.CustTextArea;
import us.icitap.commandcenter.ui.custom.CustTextField;
import us.icitap.commandcenter.ui.custom.NotificationTextField;

import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.server.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Field;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class UpdateVerificationWindow extends Window implements ClickListener{

	
	private final NotificationTextField notificationTextField = new NotificationTextField("Kush njoftohet per telefonaten");
	private final CustTextField patrolsTextField = new CustTextField("Patrulla qe caktohet per verifikim");
	private final CustTextField givenOrdersTextField = new CustTextField("Urdherat qe jepen");
	private final CustTextArea notesTextArea = new CustTextArea("Shenime mbi verifikimin e telefonates");
	private final CustSaveButton saveButton = new CustSaveButton("RUAJ");
	
	CustBeanFieldGroup<CallManagement> binder = new CustBeanFieldGroup<CallManagement>(CallManagement.class);
	private Call call;
	private CallManagement callManagement;
	
	private UpdateCallListener updateCallListener;

	ThemeResource infoImgResource = new ThemeResource("img/icons/blue/Info.png");
	
	final Embedded embPreviousNotifications = new Embedded(null,infoImgResource);
	final Embedded embPreviousPatrols = new Embedded(null,infoImgResource);
	final Embedded embPreviousVerNotes = new Embedded(null, infoImgResource);
	final Embedded embPrevoiusGivenOrders = new Embedded(null,infoImgResource);
	

	
	public UpdateVerificationWindow(UpdateCallListener updateCallListener,Call call) {
		
		this.updateCallListener = updateCallListener;
	
		this.setCall(call);

		this.setCaption("Te dhena shtese per telefonaten");
		this.setModal(true);
		this.center();
		this.setResizable(false);

		this.binder.bind(this.notificationTextField, "whoIsNotified");
		this.binder.bind(this.patrolsTextField, "patrols");
		this.binder.bind(this.givenOrdersTextField, "givenOrders");
		this.binder.bind(this.notesTextArea, "notes");
		
		VerticalLayout vl = new VerticalLayout();
		vl.setMargin(true);
		
		this.patrolsTextField.setDescription("Shtoni patrullat qe dergohen");
		this.givenOrdersTextField.setDescription("Shtoni urdherat qe jepen");
		this.notesTextArea.setDescription("Shtoni shenimet shtese ne lidhje me telefonaten dhe verifikimin");
		
		
		HorizontalLayout hl0 = new HorizontalLayout();
		
		hl0.addComponent(notificationTextField);
		hl0.addComponent(embPreviousNotifications);
		
		HorizontalLayout hl1 = new HorizontalLayout();
		
		hl1.addComponent(patrolsTextField);
		hl1.addComponent(embPreviousPatrols);
		

		HorizontalLayout hl2 = new HorizontalLayout();
		hl2.addComponent(notesTextArea);
		hl2.addComponent(embPreviousVerNotes);
		this.notesTextArea.setWidth("300px");
		
		HorizontalLayout hl3 = new HorizontalLayout();
		hl3.addComponent(givenOrdersTextField);
		hl3.addComponent(embPrevoiusGivenOrders);
		this.givenOrdersTextField.setWidth("300");
		
		vl.addComponent(hl0);
		vl.addComponent(hl1);
		vl.addComponent(hl3);
		vl.addComponent(hl2);
		
		
		final ButtonsLayout buttonsLayout = new ButtonsLayout("500px");
		buttonsLayout.addComponent(saveButton);
		buttonsLayout.setComponentAlignment(saveButton, Alignment.MIDDLE_RIGHT);
		vl.addComponent(buttonsLayout);
		
		
		this.setContent(vl);
		saveButton.setImmediate(true);
		saveButton.setDescription("Ne momentin qe do shtypni butonin te dhenat do ruhen ne DB");
		this.saveButton.addClickListener(this);
	
		this.setCallManagementInfo(call);

	}
	
	
	private void setCallManagementInfo(Call call){
		if(call == null) return;
		this.embPreviousNotifications.setDescription(call.getNotificationsInfo());
		this.embPreviousPatrols.setDescription(call.getPatrolsInfo());
		this.embPrevoiusGivenOrders.setDescription(call.getGivenOrdersInfo());
		this.embPreviousVerNotes.setDescription(call.getNotesInfoForHtml());
	}
	
	public void setCall(Call call){
		this.call = call;
		this.callManagement = new CallManagement(call);
		this.setCallManagementInfo(call);
		this.binder.setItemDataSource(callManagement);
	}
	
	
	@Override
	public void buttonClick(ClickEvent event) {
		try {
			binder.commit();
			callManagement.setRecordDate(new Date());
			callManagement.setRecordUser(CommandCenterUI.getUser().getUsername());
			
			if(call.getCallManagementList() == null)
				call.setCallManagementList(new ArrayList<CallManagement>());
			call.getCallManagementList().add(callManagement);
			this.updateCallListener.updateCallInformation(call);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
